package cn.lili.timetask.handler.impl.order;

import cn.hutool.core.util.StrUtil;
import cn.lili.cache.impl.RedisCache;
import cn.lili.modules.constant.CommonConstant;
import cn.lili.modules.order.order.client.OrderClient;
import cn.lili.timetask.handler.EveryDayExecute;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Component
@RequiredArgsConstructor
public class CityDistributionTaskExecute implements EveryDayExecute {

    private final OrderClient orderClient;
    @Resource
    private RedisCache redisCache;

    //执行日期
    private Integer upgrade_citydis_task_date = 15;
    //执行时间
    private Integer upgrade_citydis_task_time = 0;
    /**
     * 每月15号00点执行店主升级为城主
     */
    @Override
    public void execute() {
        String upgrade_citydis_task_dateStr = redisCache.getString(CommonConstant.UPGRADE_CITYDIS_TASK_DATE);
        String upgrade_citydis_task_timeStr = redisCache.getString(CommonConstant.UPGRADE_CITYDIS_TASK_TIME);
        //执行日期
        if (StrUtil.isNotBlank(upgrade_citydis_task_dateStr)) {
            upgrade_citydis_task_date = Integer.valueOf(upgrade_citydis_task_dateStr);
        }
        //执行时间
        if (StrUtil.isNotBlank(upgrade_citydis_task_timeStr)) {
            upgrade_citydis_task_time = Integer.valueOf(upgrade_citydis_task_timeStr);
        }

        LocalDateTime currentDate = LocalDateTime.now();
        //每月15号00点执行
        if (currentDate.getDayOfMonth() == upgrade_citydis_task_date && currentDate.getHour() == upgrade_citydis_task_time) {
            orderClient.executeCityDistributionLastMonthOrder();
        }
    }
}
